<template>
  <div>
    <van-nav-bar :title="`当前城市-${root.area}`" @click-left="$router.push('/')">
      <van-icon name="cross" slot="left" />
    </van-nav-bar>
    <div class="p-10">
      <router-link
        tag="div"
        to="/city/search"
        class="bg-block-grey text-grey flex j-between a-center fs-rem-13px p-v-5 p-h-10 b-r-20"
      >
        <van-icon name="search" />
        <p>城市名</p>
        <!-- 占位, 不要删除 -->
        <p></p>
      </router-link>
    </div>
    <van-skeleton :row="5" :loading="!ready">
      <van-index-bar highlight-color="#fd4f51" :index-list="indexList">
        <div v-for="(item, key) in list" :key="key">
          <van-index-anchor :index="key" />
          <van-cell
            @click="handleChooseCity(city)"
            v-for="city in item"
            :key="city.id"
            :title="city.name"
          />
        </div>
      </van-index-bar>
    </van-skeleton>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      ready: false,
      list: [],
      indexList: []
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      this.getAllCity()
    },
    handleClickLeft () {
      console.log(1)
    },
    handleChooseCity (city) {
      console.log(city)
    },
    async getAllCity () {
      let { sysAreaList } = await this.$api.post('/area/query', {})
      this.list = this.formatCityList(sysAreaList)
      this.ready = true
    },
    formatCityList (citys) {
      let newList = {}
      let indexList = []
      citys.forEach(c => {
        // 对象key为首字母, value为城市对象
        newList[c.code] ? newList[c.code].push(c) : newList[c.code] = [c]
        indexList.push(c.code)
      })
      this.indexList = [...new Set(indexList)]
      return newList
    }
  },
  computed: {
    ...mapState(['root'])
  }
}
</script>

<style lang="scss" scoped>
</style>
